package service

import (
	"talentsystem/model"
	"github.com/gin-gonic/gin"
	"github.com/astaxie/beego/orm"
	"fmt"
)

func SystemLogin (c*gin.Context){
	var param model.Admin
	if err:=c.Bind(&param);err!=nil{
		c.JSON(500,gin.H{
			"status":500,
			"msg":"参数错误",
		})
		return
	}
	num,err:=orm.NewOrm().QueryTable("admin").Filter("user_name",param.UserName).Filter("pass_word",param.PassWord).Count()
	if err!=nil{
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错误请刷新",
		})
		return
	}
	var msg model.Admin
	if num==0{
		c.JSON(403,gin.H{
			"status":403,
			"msg":0,
		})
		return
	}else {
		err=orm.NewOrm().QueryTable("admin").Filter("user_name",param.UserName).Filter("pass_word",param.PassWord).One(&msg,"id")
		if err!=nil{
			c.JSON(500,gin.H{
				"status":500,
				"msg":"系统错误请刷新",
			})
			return
		}
	}
/*	session := sessions.Default(c)
	session.Set("admin", msg.Id)
	session.Save()*/
	c.JSON(200,gin.H{
		"status":200,
		"msg":1,
		"thisid":msg.Id,
	})
	return
}
func InsertAdmin(c*gin.Context){
	num,err:=orm.NewOrm().QueryTable("admin").Filter("user_name","admin").Count()
	if err!=nil {
		fmt.Println("查询账号失败")
		return
	}
	if num>0{
		fmt.Println("管理员已生成")
		return
	}
	admin:=new(model.Admin)
	admin.UserName="admin"
	admin.PassWord="admin0129"
	_,err=orm.NewOrm().Insert(admin)
	if err!=nil {
		fmt.Println("更新账号失败")
		return
	}
	fmt.Println("管理员已生成")
}